package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.entityVo.OrderVo;
import com.antdesignbackend.backend.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    OrderService orderService;
    /**
     *标签管理
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "order/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody OrderVo orderVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<OrderVo> orderVos=new ArrayList<>();
        QueryWrapper<OrderVo> wrapper =new QueryWrapper<>();
        System.out.println("*******************************前端传来的商户名称："+orderVo.getShopName());
        if(orderVo.getShopName()!=null&& !orderVo.getShopName().equals(""))
        {
            System.out.println("进入if语句");
            wrapper.like("E.name",orderVo.getShopName());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=orderService.count(wrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            orderVos=orderService.search(wrapper,new Page<>(orderVo.getPageIndex(),orderVo.getPageSize()));
        }
        result.setData(orderVos);
        return result;
    }

}
